package com.cloudcc.mobile.util;

import java.io.File;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.TypedValue;

/**
 * 图片工具类
 * 
 * @author 水寒 欢迎访问水寒的个人博客：http://www.sunhome.org.cn
 * 
 */
public class ImageUtil {

	/**
	 * 设置水印图片在左上角
	 * 
	 * @param Context
	 * @param src
	 * @param watermark
	 * @param paddingLeft
	 * @param paddingTop
	 * @return
	 */
	public static Bitmap createWaterMaskLeftTop(Context context, Bitmap src,
			Bitmap watermark, int paddingLeft, int paddingTop) {
		return createWaterMaskBitmap(src, watermark,
				dp2px(context, paddingLeft), dp2px(context, paddingTop));
	}

	private static Bitmap createWaterMaskBitmap(Bitmap src, Bitmap watermark,
			int paddingLeft, int paddingTop) {
		if (src == null) {
			return null;
		}
		int width = src.getWidth();
		int height = src.getHeight();
		// 创建一个bitmap
		Bitmap newb = Bitmap.createBitmap(width, height, Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
		// 将该图片作为画布
		Canvas canvas = new Canvas(newb);
		// 在画布 0，0坐标上开始绘制原始图片
		canvas.drawBitmap(src, 0, 0, null);
		// 在画布上绘制水印图片
		canvas.drawBitmap(watermark, paddingLeft, paddingTop, null);
		// 保存
//		canvas.save(Canvas.ALL_SAVE_FLAG);
		canvas.save();
		// 存储
		canvas.restore();
		return newb;
	}

	/**
	 * 设置水印图片在右下角
	 * 
	 * @param Context
	 * @param src
	 * @param watermark
	 * @param paddingRight
	 * @param paddingBottom
	 * @return
	 */
	public static Bitmap createWaterMaskRightBottom(Context context,
			Bitmap src, Bitmap watermark, int paddingRight, int paddingBottom) {
		return createWaterMaskBitmap(
				src,
				watermark,
				src.getWidth() - watermark.getWidth()
						- dp2px(context, paddingRight), src.getHeight()
						- watermark.getHeight() - dp2px(context, paddingBottom));
	}

	/**
	 * 设置水印图片到右上角
	 * 
	 * @param Context
	 * @param src
	 * @param watermark
	 * @param paddingRight
	 * @param paddingTop
	 * @return
	 */
	public static Bitmap createWaterMaskRightTop(Context context, Bitmap src,
			Bitmap watermark, int paddingRight, int paddingTop) {
		return createWaterMaskBitmap(
				src,
				watermark,
				src.getWidth() - watermark.getWidth()
						- dp2px(context, paddingRight),
				dp2px(context, paddingTop));
	}

	/**
	 * 设置水印图片到左下角
	 * 
	 * @param Context
	 * @param src
	 * @param watermark
	 * @param paddingLeft
	 * @param paddingBottom
	 * @return
	 */
	public static Bitmap createWaterMaskLeftBottom(Context context, Bitmap src,
			Bitmap watermark, int paddingLeft, int paddingBottom) {
		return createWaterMaskBitmap(
				src,
				watermark,
				dp2px(context, paddingLeft),
				src.getHeight() - watermark.getHeight()
						- dp2px(context, paddingBottom));
	}

	/**
	 * 设置水印图片到中间
	 * 
	 * @param Context
	 * @param src
	 * @param watermark
	 * @return
	 */
	public static Bitmap createWaterMaskCenter(Bitmap src, Bitmap watermark) {
		return createWaterMaskBitmap(src, watermark,
				(src.getWidth() - watermark.getWidth()) / 2,
				(src.getHeight() - watermark.getHeight()) / 2);
	}

	/**
	 * 给图片添加文字到左上角
	 * 
	 * @param context
	 * @param bitmap
	 * @param text
	 * @return
	 */
	public static Bitmap drawTextToLeftTop(Context context, Bitmap bitmap,
			String text, int size, int color, int paddingLeft, int paddingTop) {
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		paint.setColor(color);
		paint.setTextSize(dp2px(context, size));
		Rect bounds = new Rect();
		paint.getTextBounds(text, 0, text.length(), bounds);
		return drawTextToBitmap(context, bitmap, text, paint, bounds,
				dp2px(context, paddingLeft), dp2px(context, paddingTop)
						+ bounds.height());
	}

	/**
	 * 绘制文字到右下角
	 * 
	 * @param context
	 * @param bitmap
	 * @param text
	 * @param size
	 * @param color
	 * @param paddingLeft
	 * @param paddingTop
	 * @return
	 */
	public static Bitmap drawTextToRightBottom(Context context, Bitmap bitmap,
			String text, int size, int color, int paddingRight,
			int paddingBottom) {
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		paint.setColor(color);
		paint.setTextSize(dp2px(context, size));
		Rect bounds = new Rect();
		paint.getTextBounds(text, 0, text.length(), bounds);
		return drawTextToBitmap(
				context,
				bitmap,
				text,
				paint,
				bounds,
				bitmap.getWidth() - bounds.width()
						- dp2px(context, paddingRight), bitmap.getHeight()
						- dp2px(context, paddingBottom));
	}

	/**
	 * 绘制文字到右上方
	 * 
	 * @param context
	 * @param bitmap
	 * @param text
	 * @param size
	 * @param color
	 * @param paddingRight
	 * @param paddingTop
	 * @return
	 */
	public static Bitmap drawTextToRightTop(Context context, Bitmap bitmap,
			String text, int size, int color, int paddingRight, int paddingTop) {
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		paint.setColor(color);
		paint.setTextSize(dp2px(context, size));
		Rect bounds = new Rect();
		paint.getTextBounds(text, 0, text.length(), bounds);
		return drawTextToBitmap(
				context,
				bitmap,
				text,
				paint,
				bounds,
				bitmap.getWidth() - bounds.width()
						- dp2px(context, paddingRight),
				dp2px(context, paddingTop) + bounds.height());
	}

	/**
	 * 绘制文字到左下方
	 * 
	 * @param context
	 * @param bitmap
	 * @param text
	 * @param size
	 * @param color
	 * @param paddingLeft
	 * @param paddingBottom
	 * @return
	 */
	public static Bitmap drawTextToLeftBottom(Context context, Bitmap bitmap,
			String text, int size, int color, int paddingLeft, int paddingBottom) {
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		paint.setColor(color);
		//paint.setTextSize(TypedValue.COMPLEX_UNIT_PX);
		//int size2=50;
		// paint.setTextSize(dp2px(context, size2));
		 paint.setTextSize(140);
		//paint.setTypeface(Typeface.DEFAULT);//设置字体类型
		Rect bounds = new Rect();
		paint.getTextBounds(text, 0, text.length(), bounds);
		return drawTextToBitmap(context, bitmap, text, paint, bounds,
				dp2px(context, paddingLeft),
				bitmap.getHeight() - dp2px(context, paddingBottom));
	}

	/**
	 * 绘制文字到中间
	 * 
	 * @param context
	 * @param bitmap
	 * @param text
	 * @param size
	 * @param color
	 * @return
	 */
	public static Bitmap drawTextToCenter(Context context, Bitmap bitmap,
			String text, int size, int color) {
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		paint.setColor(color);
		paint.setTextSize(dp2px(context, size));
		Rect bounds = new Rect();
		paint.getTextBounds(text, 0, text.length(), bounds);
		return drawTextToBitmap(context, bitmap, text, paint, bounds,
				(bitmap.getWidth()) / 10,
				(bitmap.getHeight() + bounds.height()) / 2);
	}

	public static Bitmap drawTextToCenter1(Context context, Bitmap bitmap,
			String text, int size, int color) {
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		paint.setColor(color);
		paint.setTextSize(dp2px(context, size));
		Rect bounds = new Rect();
		paint.getTextBounds(text, 0, text.length(), bounds);
		return drawTextToBitmap(context, bitmap, text, paint, bounds,
				(bitmap.getWidth()) / 10,
				(bitmap.getHeight() + bounds.height()) / 2 + size * 4);
	}

	public static Bitmap drawTextToCenter2(Context context, Bitmap bitmap,
			String text, int size, int color) {
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		paint.setColor(color);
		paint.setTextSize(dp2px(context, size));
		Rect bounds = new Rect();
		paint.getTextBounds(text, 0, text.length(), bounds);
		return drawTextToBitmap(context, bitmap, text, paint, bounds,
				(bitmap.getWidth()) / 10,
				(bitmap.getHeight() + bounds.height()) / 2 + size * 7);
	}

	public static Bitmap drawTextToCenter3(Context context, Bitmap bitmap,
			String text, int size, int color) {
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		paint.setColor(color);
		paint.setTextSize(dp2px(context, size));
		Rect bounds = new Rect();
		paint.getTextBounds(text, 0, text.length(), bounds);
		return drawTextToBitmap(context, bitmap, text, paint, bounds,
				(bitmap.getWidth()) / 10,
				(bitmap.getHeight() + bounds.height()) / 2 + size * 10);
	}

	// 图片上绘制文字
	private static Bitmap drawTextToBitmap(Context context, Bitmap bitmap,
			String text, Paint paint, Rect bounds, int paddingLeft,
			int paddingTop) {
		Config bitmapConfig = bitmap.getConfig();

		paint.setDither(true); // 获取跟清晰的图像采样
		paint.setFilterBitmap(true);// 过滤一些
		if (bitmapConfig == null) {
			bitmapConfig = Config.ARGB_8888;
		}
		bitmap = bitmap.copy(bitmapConfig, true);
		Canvas canvas = new Canvas(bitmap);
		Paint paint1=new Paint();
		paint1.setTextSize(80);
		canvas.drawText(text, paddingLeft, paddingTop, paint);
		return bitmap;
	}

	/**
	 * 缩放图片
	 * 
	 * @param src
	 * @param w
	 * @param h
	 * @return
	 */
	public static Bitmap scaleWithWH(Bitmap src, double w, double h) {
		if (w == 0 || h == 0 || src == null) {
			return src;
		} else {
			// 记录src的宽高
			int width = src.getWidth();
			int height = src.getHeight();
			// 创建一个matrix容器
			Matrix matrix = new Matrix();
			// 计算缩放比例
			float scaleWidth = (float) (w / width);
			float scaleHeight = (float) (h / height);
			// 开始缩放
			matrix.postScale(scaleWidth, scaleHeight);
			// 创建缩放后的图片
			return Bitmap.createBitmap(src, 0, 0, width, height, matrix, true);
		}
	}

	/**
	 * dip转pix
	 * 
	 * @param context
	 * @param dp
	 * @return
	 */
	public static int dp2px(Context context, float dp) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dp * scale + 0.5f);
	}

	/**
	 * 获取本地图片并指定高度和宽度
	 */
	public static Bitmap getNativeImage(String imagePath) {
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		// 获取这个图片的宽和高
		Bitmap myBitmap = BitmapFactory.decodeFile(imagePath, options); // 此时返回myBitmap为空
		// 计算缩放比
		int be = (int) (options.outHeight / (float) 200);
		int ys = options.outHeight % 200;// 求余数
		float fe = ys / (float) 200;
		if (fe >= 0.5)
			be = be + 1;
		if (be <= 0)
			be = 1;
		options.inSampleSize = be;
		// 重新读入图片，注意这次要把options.inJustDecodeBounds 设为 false
		options.inJustDecodeBounds = false;
		myBitmap = BitmapFactory.decodeFile(imagePath, options);
		return myBitmap;
	}

	/**
	 * 以最省内存的方式读取本地资源的图片 或者SDCard中的图片
	 * 
	 * @param imagePath
	 *            图片在SDCard中的路径
	 * @return
	 */
	public static Bitmap getSDCardImg(String imagePath) {
		BitmapFactory.Options opt = new BitmapFactory.Options();
		opt.inPreferredConfig = Config.RGB_565;
		opt.inPurgeable = true;
		opt.inInputShareable = true;
		// 获取资源图片
		return BitmapFactory.decodeFile(imagePath, opt);
	}

	// 读取本地图片
	public static Bitmap getDiskBitmap(String pathString) {
		Bitmap bitmap = null;
		try {
			File file = new File(pathString);
			if (file.exists()) {
				bitmap = BitmapFactory.decodeFile(pathString);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}

		return bitmap;
	}
}
